package com.xyy.saas.payment.service.excel;

import com.xyy.saas.payment.exception.PaymentException;
import com.xyy.saas.payment.manage.cores.vo.ResultVO;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;

public abstract class AbstractAnalysisFile implements AnalysisFile {

    public void analysisExcel(MultipartFile file, HttpServletResponse response) throws PaymentException {

        ResultVO checkResult = checkFile(file);
        if(!checkResult.isSuccess()) {
            throw new PaymentException(checkResult.getMsg());
        }

        Map<String, ArrayList<String[]>> hashMap = analysisFile(file);

        checkResult = checkData(hashMap);
        if(!checkResult.isSuccess()) {
            throw new PaymentException(checkResult.getMsg());
        }

        Map<String, ArrayList<String[]>> result = processData(hashMap);

        processExcel(result, response);
    }

    protected abstract Map<String, ArrayList<String[]>> processData(Map<String, ArrayList<String[]>> hashMap);

    protected abstract ResultVO checkData(Map<String, ArrayList<String[]>> hashMap);

    protected abstract Map<String, ArrayList<String[]>> analysisFile(MultipartFile file) throws PaymentException;

    protected abstract ResultVO checkFile(MultipartFile file);

    protected abstract void processExcel(Map<String, ArrayList<String[]>> result, HttpServletResponse response) throws PaymentException;

}
